package com.community.zhihuishequ.view.home;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.view.base.BaseHostActivity;
import com.tencent.bugly.beta.Beta;

import org.apache.commons.lang3.ArrayUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 主页（首页）
 */
public class HomeActivity extends BaseHostActivity {

    @BindView(R.id.tv_subsystem)
    TextView tvSubsystem;
    @BindView(R.id.rl_subsystem)
    RelativeLayout rlSubsystem;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_unread)
    TextView tvUnread;
    @BindView(R.id.rl_message)
    FrameLayout rlMessage;
    @BindView(R.id.tv_setting)
    TextView tvSetting;
    @BindView(R.id.rl_setting)
    RelativeLayout rlSetting;
    @BindView(R.id.ablTabs)
    AppBarLayout ablTabs;
    @BindView(R.id.container)
    FrameLayout container;

    private boolean subSystemIsClick = false;
    private boolean messageIsClick = false;
    private boolean settingIsClick = false;
    /**
     * 点击两次退出
     */
    private boolean isTwiveBack;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        unbinder = ButterKnife.bind(this);
        transFragment(getString(R.string.home_tab1));
        subSystemIsClick = true;
        onRlSubsystemClicked();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Beta.checkUpgrade();
    }

    @Override
    public Fragment createFragment(String fragmentTag) {
        String[] tabs = getResources().getStringArray(R.array.home_tabs);
        switch (ArrayUtils.indexOf(tabs, fragmentTag)) {
            case 0:
                return new SubSystemFragment();
        }
        return null;
    }

    @Override
    public int getContainerId() {
        return R.id.container;
    }

    @OnClick(R.id.rl_subsystem)
    public void onRlSubsystemClicked() {
        tvSubsystem.setSelected(true);
        tvSubsystem.setTextColor(getResources().getColor(R.color.colorPrimary));
        tvMessage.setSelected(false);
        tvMessage.setTextColor(getResources().getColor(R.color.textBlack));
        tvSetting.setSelected(false);
        tvSetting.setTextColor(getResources().getColor(R.color.textBlack));
        if (!subSystemIsClick) {
            transFragment(getResources().getString(R.string.home_tab1));
            subSystemIsClick = true;
            messageIsClick = false;
            settingIsClick = false;
        }
    }

    @OnClick(R.id.rl_message)
    public void onRlMessageClicked() {
        tvSubsystem.setSelected(false);
        tvSubsystem.setTextColor(getResources().getColor(R.color.textBlack));
        tvMessage.setSelected(true);
        tvMessage.setTextColor(getResources().getColor(R.color.colorPrimary));
        tvSetting.setSelected(false);
        tvSetting.setTextColor(getResources().getColor(R.color.textBlack));
        if (!messageIsClick) {
            transFragment(getResources().getString(R.string.home_tab2));
            subSystemIsClick = false;
            messageIsClick = true;
            settingIsClick = false;
        }
    }

    @OnClick(R.id.rl_setting)
    public void onRlSettingClicked() {
        tvSubsystem.setSelected(false);
        tvSubsystem.setTextColor(getResources().getColor(R.color.textBlack));
        tvMessage.setSelected(false);
        tvMessage.setTextColor(getResources().getColor(R.color.textBlack));
        tvSetting.setSelected(true);
        tvSetting.setTextColor(getResources().getColor(R.color.colorPrimary));
        if (!settingIsClick) {
            transFragment(getResources().getString(R.string.home_tab3));
            subSystemIsClick = false;
            messageIsClick = false;
            settingIsClick = true;
        }
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isTwiveBack) {
                isTwiveBack = true;
                ToastUtils.showShort("再按一次退出程序");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isTwiveBack = false;
                    }
                }, 1000);
            } else {
                finish();
            }
            return false;
        }
        return super.onKeyLongPress(keyCode, event);
    }
}
